package com.my12306.user.controller.auth;


import com.my12306.common.util.PageUtils;
import com.my12306.common.util.response.ResponseData;
import com.my12306.user.model.po.PUserEntity;
import com.my12306.user.model.po.auth.PermissionEntity;
import com.my12306.user.model.vo.PermissionVo;
import com.my12306.user.service.PUserService;
import com.my12306.user.service.auth.PermissionService;
import com.my12306.user.util.auth.OauthUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Author:苏牧夕
 * @Date:2020-05-15 17:36:13
 * @Version 1.0
 */
@RestController
@RequestMapping("auth/permission")
@Api(value = "权限管理接口", tags = "权限管理接口")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private OauthUtils oauthUtils;
    @Autowired
    private PUserService pUserService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public ResponseData list(@RequestParam Map<String, Object> params) {

        PageUtils page = permissionService.queryPage(params);

        return ResponseData.ok().put("page", page);
    }

    /**
     * 查询所有权限
     *
     * @return
     */
    @GetMapping("/allList")
    @ApiOperation(value = "查询所有权限", httpMethod = "GET", response = ResponseData.class)
    public ResponseData list() {
//        System.out.println("===========》" + oauthUtils.getUserPrincipal());
        List<PermissionEntity> permissionEntities = permissionService.list();
        for (PermissionEntity entity : permissionEntities) {
            PermissionEntity parentMenuEntity = permissionService.getById(entity.getParentId());
            if (parentMenuEntity != null) {
                entity.setParentName(parentMenuEntity.getName());
            }
        }
        System.out.println(permissionEntities.get(permissionEntities.size() - 1));
        return ResponseData.ok().put("data", permissionEntities);
    }

    /**
     * 查询所有非按钮级菜单
     *
     * @return
     */
    @GetMapping("/selectList")
    @ApiOperation(value = "查询所有非按钮级菜单", httpMethod = "GET", response = ResponseData.class)
    public ResponseData selectlist() {
        List<PermissionVo> permissionEntities = permissionService.getPermsNotButtonList();
        PermissionVo root = new PermissionVo();
        root.setMenuId(0L);
        root.setName("一级菜单");
        root.setParentId(-1L);
        root.setOpen(true);
        permissionEntities.add(root);
        return ResponseData.ok().put("menuList", permissionEntities);
    }

    @GetMapping("/dynamicMenu")
    @ApiOperation(value = "查询树形菜单", httpMethod = "GET", response = ResponseData.class)
    public ResponseData getMenue(Long user_id) {
        PUserEntity userInfo = null;
        if (user_id == null) {
            String username = oauthUtils.getUserInfo();
            System.out.println(username);
            userInfo = pUserService.getUserInfoByUsername(username);
        }
        if (userInfo != null)
            user_id = userInfo.getId();
        if (user_id == null) return ResponseData.fail();
        List<PermissionEntity> permissionList = permissionService.getUserPermListByUser_id(user_id);

//         System.out.println("=======树形菜单========");
//        System.out.println(permissionList);
        List<String> permission = permissionService.getUserPermissionByUser_id(user_id);

        List<String> perms = new ArrayList<>();
        permission.stream().filter(x -> StringUtils.isNotEmpty(x)).forEach(x -> {
            List<String> split = Arrays.asList(x.split(","));
            perms.addAll(split);
        });
//        System.out.println("=======权限========");
//        System.out.println(perms);
        return ResponseData.ok().put("menuList", permissionList).put("permissions", perms);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{permId}")
    @ApiOperation(value = "根据权限idc查询详细信息", httpMethod = "GET", response = ResponseData.class)
    public ResponseData info(@ApiParam(value = "权限id", name = "permId") @PathVariable("permId") Long permId) {
        PermissionEntity oauthPermission = permissionService.getById(permId);
        System.out.println(oauthPermission);
        return ResponseData.ok().put("data", oauthPermission);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation(value = "添加一个权限", httpMethod = "POST", response = ResponseData.class)
    public ResponseData save(@RequestBody PermissionVo vo) {


        permissionService.savePermission(vo);
        return ResponseData.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改一个权限", httpMethod = "PUT", response = ResponseData.class)
    public ResponseData update(@RequestBody PermissionEntity oauthPermission) {
        permissionService.updateById(oauthPermission);

        return ResponseData.ok();
    }

    /**
     * 删除权限
     * 1.删除权限表自己本身
     * 2.删除相关子权限
     * 3.删除 角色-权限相关
     */
    @DeleteMapping("/delete/{permId}")
    @ApiOperation(value = "删除权限", httpMethod = "DELETE", response = ResponseData.class)
    public ResponseData delete(@ApiParam(value = "权限id", name = "permId") @PathVariable("permId") Long permId) {
        permissionService.deletePermById(permId);
        return ResponseData.ok();
    }

}
